package cn.ncist.health_service_provider.redis;

import cn.ncist.pojo.Setmeal;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@Component
public class UpdateRedisInfo {
    /**
     * 套餐删除数据时
     * 更新redis内的数据
     * @param setmealList,id
     */
    public List<Setmeal> delete(List<Setmeal> setmealList, Integer id){
        int i = -1;
        for (Setmeal setmeal : setmealList) {
            i++;
            if(setmeal.getId().equals(id)){
                setmealList.remove(i);
                break;
            }
        }
        log.info("删除套餐数据更新redis缓存成功！！！");
        return setmealList;
    }

    /**
     * 添加套餐数据时
     * 更新redis缓存
     * @param setmealList,setemal
     * @return
     */
    public List<Setmeal> add(List<Setmeal> setmealList, Setmeal setmeal){
        setmealList.add(setmeal);
        log.info("添加套餐数据更新redis缓存成功！！！");
        return setmealList;
    }

    /**
     * 更新套餐数据时
     * 更新redis缓存
     * @param setmealList
     * @param setmeal
     * @return
     */
    public List<Setmeal> update(List<Setmeal> setmealList, Setmeal setmeal){
        Integer setmealId = setmeal.getId();
        int i = -1;
        for (Setmeal setmealInList : setmealList) {
            i++;
            if(setmealId.equals(setmealInList.getId())){
                setmealList.remove(i);
                setmealList.add(setmealInList);
            }
        }
        log.info("更新套餐数据更新redis缓存成功！！！");
        return setmealList;
    }
}
